home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 9 / develop 9 code / Color on 1-Bit Devices / DemoRoutines.c < prev    next >
Encoding:
Text File  |  1992-01-06  |  5.1 KB  |  213 lines  |  [TEXT/KAHL]

  1. /* Prototypes */
  2. void        MakeIndexedPict2(void);
  3. void        MakeDirectPict2(void);
  4. void        MakeHalftone(void);
  5. PicHandle     CreatePICT2( PixMap *srcBits, Rect *srcRect, Rect *dstRect, short mode );
  6. PicHandle     HalftonePixMap(PixMapHandle hSource, Boolean qdPixMap, short    Resolution);
  7.  
  8. #define TESTROWBYTES 20
  9.  
  10. /* external System version global */
  11. extern short    gSystemVersion;
  12.  
  13.  
  14. void
  15. MakeIndexedPict2(void)
  16. {
  17. PicHandle myPic;
  18. PixMap srcBits;
  19. Rect myRect, offBounds = {0, 0, 200, 200};
  20. long    myErr;
  21. short * bitsPtr;
  22. short iii;
  23.  
  24.  
  25. /* Build a nice PixMap */
  26.     srcBits.baseAddr = (Ptr)NewPtr( TESTROWBYTES*4 );    /* rowbytes = TESTROWBYTES, height = 4 */
  27.     /* Cheesy error checking */
  28.     if(srcBits.baseAddr == nil) { SysBeep(10); return; }
  29.     srcBits.rowBytes = TESTROWBYTES | 0x8000;    /* pixmap */
  30.     srcBits.bounds.top = 0;
  31.     srcBits.bounds.left = 0;
  32.     srcBits.bounds.bottom = 4;
  33.     srcBits.bounds.right = TESTROWBYTES;
  34.     srcBits.pmVersion = 0;
  35.     srcBits.packType = 0;
  36.     srcBits.packSize = 0;
  37.     srcBits.hRes = 0x480000;    /* 72, fixed point */
  38.     srcBits.vRes = 0x480000;
  39.     srcBits.pixelType = 0;
  40.     srcBits.pixelSize = 8;
  41.     srcBits.cmpCount = 1;
  42.     srcBits.cmpSize = 8;
  43.     srcBits.planeBytes = 0;
  44.     srcBits.pmTable = GetCTable( 8 );
  45.     /* Cheesy error checking */
  46.     if(srcBits.pmTable == nil) {SysBeep(10); DisposPtr(srcBits.baseAddr); return;}
  47.     srcBits.pmReserved = 0;
  48.     
  49.     /* Load an image */    
  50.     bitsPtr = (short *) srcBits.baseAddr;
  51.     for( iii = TESTROWBYTES*2; iii > 0; iii-- )
  52.         *bitsPtr++ = iii*0x105;
  53.         
  54.     SetRect( &myRect, 0, 0, TESTROWBYTES, 4 );    /*left, top, right, bottom */
  55.     
  56.     myPic = CreatePICT2( &srcBits, &myRect, &offBounds, ditherCopy );
  57.     if( !myPic )
  58.     {
  59.         SysBeep(10);
  60.         DisposPtr(srcBits.baseAddr);
  61.         DisposCTable(srcBits.pmTable);
  62.         return;
  63.     }
  64.         
  65. //    DrawPicture( myPic, &(**myPic).picFrame);
  66.     myRect = qd.thePort->portRect;    /*left, top, right, bottom */
  67.     DrawPicture( myPic, &myRect);
  68.     
  69.     ZeroScrap();
  70.     myErr = PutScrap( GetHandleSize( myPic ), 'PICT', *myPic );
  71.     
  72.     DisposCTable( srcBits.pmTable );        /* Dispose of color table */
  73.     DisposPtr( srcBits.baseAddr );
  74.     DisposHandle( (Handle) myPic );
  75. }
  76.  
  77. void
  78. MakeDirectPict2(void)
  79. {
  80. PicHandle myPic;
  81. PixMap srcBits;
  82. Rect myRect, offBounds = {0, 0, 200, 200};
  83. long    myErr;
  84. short * bitsPtr;
  85. short iii;
  86.  
  87.  
  88. /* Build a nice PixMap */
  89.     srcBits.baseAddr = (Ptr)NewPtr( TESTROWBYTES*4*4 );    /* rowbytes = TESTROWBYTES, height = 4, 4bytes/pixel */
  90.     /* Cheesy error checking */
  91.     if(srcBits.baseAddr == nil) { SysBeep(10); return; }
  92.     srcBits.rowBytes = (TESTROWBYTES *4) | 0x8000;    /* pixmap */
  93.     srcBits.bounds.top = 0;
  94.     srcBits.bounds.left = 0;
  95.     srcBits.bounds.bottom = 4;
  96.     srcBits.bounds.right = TESTROWBYTES;
  97.     srcBits.pmVersion = 0;
  98.     srcBits.packType = 0;
  99.     srcBits.packSize = 0;
  100.     srcBits.hRes = 0x480000;
  101.     srcBits.vRes = 0x480000;
  102.     srcBits.pixelType = 16;
  103.     srcBits.pixelSize = 32;
  104.     srcBits.cmpCount = 3;
  105.     srcBits.cmpSize = 8;
  106.     srcBits.planeBytes = 0;
  107.     srcBits.pmTable = 0;        // No color table in a direct PixMap
  108.     srcBits.pmReserved = 0;
  109.     
  110.     bitsPtr = (short *) srcBits.baseAddr;
  111.     for( iii = TESTROWBYTES*2*4; iii > 0; iii-- )
  112.         *bitsPtr++ = iii*0x105;
  113.         
  114.     SetRect( &myRect, 0, 0, TESTROWBYTES, 4 );    /*left, top, right, bottom */
  115.     
  116.     myPic = CreatePICT2( &srcBits, &myRect, &offBounds, ditherCopy );
  117.     if( !myPic )
  118.     {
  119.         SysBeep(10);
  120.         DisposPtr(srcBits.baseAddr);
  121.         return;
  122.     }
  123.         
  124. //    DrawPicture( myPic, &(**myPic).picFrame);
  125.     myRect = qd.thePort->portRect;    /*left, top, right, bottom */
  126.     DrawPicture( myPic, &myRect);
  127.     
  128.     ZeroScrap();
  129.     myErr = PutScrap( GetHandleSize( myPic ), 'PICT', *myPic );
  130.     
  131.     DisposPtr(srcBits.baseAddr);
  132.     DisposHandle( (Handle) myPic );
  133. }
  134.  
  135.  
  136. void
  137. MakeHalftone()
  138. {
  139. PicHandle myPic;
  140. PicHandle myPic1;
  141. PixMap srcBits;
  142. Rect myRect;
  143. Rect thisRect;
  144. long    myErr;
  145. short * bitsPtr;
  146. short iii;
  147. PixMapHandle    myPixMapHandle2;
  148. GWorldPtr        myOffGWorld2;
  149. RGBColor        saveRGB;
  150. RGBColor        myRGB;
  151. GDHandle        oldGD;
  152. GWorldPtr        oldGW;
  153.  
  154.     SetRect( &myRect, 0, 0, 256, 256 );
  155.     if( NewGWorld( &myOffGWorld2, 32, &myRect, 0, 0, 0 ) == noErr )
  156.     {
  157.         GetForeColor( &saveRGB );
  158.         GetGWorld( &oldGW,&oldGD );
  159.         SetGWorld( myOffGWorld2,nil );
  160.         
  161.         if(gSystemVersion < 0x0700)
  162.             myPixMapHandle2 = myOffGWorld2->portPixMap;        /* -----pre-7.0 */
  163.         else
  164.             myPixMapHandle2 = GetGWorldPixMap( myOffGWorld2 );  /* 7.0 only */
  165.         
  166.         LockPixels( myPixMapHandle2 );
  167.         EraseRect( &myRect );
  168. #define    NUMDIVS 10        
  169.         for( iii = 0; iii <= NUMDIVS; iii++ )
  170.         {
  171.             myRGB.red = myRGB.green = myRGB.blue =((long)iii*255/NUMDIVS) << 8;
  172.             RGBForeColor( &myRGB );
  173.             SetRect( &thisRect, iii*myRect.right/(NUMDIVS+1), 0, (iii+1)*myRect.right/(NUMDIVS+1), myRect.bottom );
  174.             PaintRect( &thisRect );
  175.         }
  176.  
  177.         myRGB.red = myRGB.green = myRGB.blue = 0;
  178.         PenMode( srcXor );
  179.         RGBForeColor( &myRGB );
  180.         FrameRect( &myRect );
  181.         
  182.         SetGWorld( oldGW, oldGD );
  183.         RGBForeColor( &saveRGB );
  184.     }
  185.     else
  186.     {
  187.         SysBeep(10);
  188.         return;
  189.     }
  190.     srcBits = **myPixMapHandle2;
  191.  
  192.     myPic = HalftonePixMap( myPixMapHandle2, true, 72 );    
  193.     if( !myPic )
  194.     {
  195.         SysBeep(10);
  196.         UnlockPixels( myPixMapHandle2 );
  197.         DisposeGWorld( myOffGWorld2 );
  198.         return;
  199.     }
  200.     DrawPicture( myPic, &(**myPic).picFrame );
  201.     
  202.     /* Add Pict to clipboard */
  203.     ZeroScrap();
  204.     myErr = PutScrap( GetHandleSize( myPic ), 'PICT', *myPic );
  205.     
  206.     /* Clean up */
  207.     UnlockPixels( myPixMapHandle2 );
  208.     DisposeGWorld( myOffGWorld2 );
  209.     DisposHandle( (Handle) myPic );
  210. }
  211.  
  212.  
  213.